package com.jiayou.activity

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import cn.hutool.core.util.ReUtil
import cn.hutool.core.util.StrUtil
import cn.hutool.http.HttpRequest
import cn.hutool.json.JSONUtil
import com.jiayou.R
import com.jiayou.tool.AndroidTool
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog
import kotlinx.android.synthetic.main.activity_login.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import java.util.*
import kotlin.concurrent.thread

class LoginActivity : AppCompatActivity(), View.OnClickListener {

    private val properties = Properties()
    private lateinit var uri: String
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        AndroidTool.fullScreen(this)

        properties.load(application.assets.open("im.properties"))
        uri = properties["im.uri"].toString() + ":" + properties["im.port"].toString()

        im_login.setOnClickListener(this)
        yonghuzhuce.setOnClickListener(this)

        initAnimation()

    }

    private fun initAnimation() {
        //头部的logo和标题
        thread {
            var i = -80
            runOnUiThread { im_img.translationX = i.toFloat() }
            for (i in i..0) {
                runOnUiThread { im_img.translationX = i.toFloat() * i.toFloat() * 2 }
                Thread.sleep(10)
            }
        }
        thread {
            var ii = 100
            runOnUiThread { im_text.translationX = ii.toFloat() }
            for (i in ii downTo 0) {
                runOnUiThread { im_text.translationX = -i.toFloat() * i.toFloat() * 2 }
                Thread.sleep(10)
            }
        }

        //输入框图标
        thread {
            val i = -130
            runOnUiThread { im_username.translationX = i.toFloat() }
            for (ii in i..0) {
                runOnUiThread { im_username.translationX = -ii.toFloat() * ii.toFloat() }
                Thread.sleep(10)
            }
        }
        thread {
            val i = -160
            runOnUiThread { im_password.translationX = i.toFloat() }
            for (ii in i..0) {
                runOnUiThread { im_password.translationX = -ii.toFloat() * ii.toFloat() }
                Thread.sleep(10)
            }
        }

        //输入框
        thread {
            val i = 140
            runOnUiThread { username.translationX = i.toFloat() }
            for (ii in i downTo 0) {
                runOnUiThread { username.translationX = ii.toFloat() * ii.toFloat() }
                Thread.sleep(10)
            }
        }
        thread {
            val i = 170
            runOnUiThread { password.translationX = i.toFloat() }
            for (ii in i downTo 0) {
                runOnUiThread { password.translationX = ii.toFloat() * ii.toFloat() }
                Thread.sleep(10)
            }
        }

        //登录图标
        thread {
            val i = 200
            runOnUiThread { im_login.translationY = i.toFloat() }
            for (ii in i downTo 0) {
                runOnUiThread { im_login.translationY = ii.toFloat() * ii.toFloat() }
                Thread.sleep(10)
            }
        }

        //忘记密码 | 用户注册
        thread {
            val i = 140
            runOnUiThread { wangjimima.translationX = i.toFloat() }
            for (ii in i downTo 0) {
                runOnUiThread { wangjimima.translationX = ii.toFloat() * ii.toFloat() }
                Thread.sleep(10)
            }
        }
        thread {
            val i = 170
            runOnUiThread { yonghuzhuce.translationX = i.toFloat() }
            for (ii in i downTo 0) {
                runOnUiThread { yonghuzhuce.translationX = ii.toFloat() * ii.toFloat() }
                Thread.sleep(10)
            }
        }
    }

    override fun onClick(p0: View) {
        when (p0.id) {
            R.id.im_login -> {
                login()
            }
            R.id.yonghuzhuce -> {
                startActivity(Intent(this, RegisterActivity::class.java))
            }
        }
    }


    //登陆逻辑
    private fun login() {
        if (username.text.toString().isEmpty() ||
            password.text.toString().isEmpty() ||
            StrUtil.isEmpty(username.text.toString()) ||
            StrUtil.isEmpty(password.text.toString())
        ) {
            Toast.makeText(this, "账户密码不能为空", Toast.LENGTH_SHORT).show()
            return
        }


        if (!ReUtil.isMatch("^\\w+\$", username.text.toString()) ||
            !ReUtil.isMatch("^\\w+\$", password.text.toString())
        ) {
            Toast.makeText(this, "账户密码不能出现非法字符", Toast.LENGTH_SHORT).show()
            return
        }


        val tip = QMUITipDialog.Builder(this)//创建tipdialog实例为其设置显示样式
            .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)//设置有加载框模式的dialog
            .setTipWord("验证中...")
            .create()
        tip.show()
        thread {
            runBlocking {
                val parse = JSONUtil.parseObj(
                    HttpRequest.get("http://$uri/im/user/login")
                        .form(
                            mapOf(
                                "username" to username.text.toString(),
                                "password" to password.text.toString()
                            )
                        )
                        .header("auth", "73913da7-508b-4646-b8ae-ef6458f41153").execute()
                        .body()
                )
                withContext(Dispatchers.Main) {
                    Toast.makeText(
                        this@LoginActivity,
                        parse["data"].toString(),
                        Toast.LENGTH_SHORT
                    )
                        .show()
                    Unit
                }
                if (parse["code"].toString().toInt() == 200) {
                    thread {
                        runBlocking {

                            val userinfo = JSONUtil.parseObj(
                                HttpRequest.get("http://$uri/im/userinfo/getUserInfo")
                                    .form(
                                        mapOf(
                                            "username" to username.text.toString()
                                        )
                                    )
                                    .header("auth", "73913da7-508b-4646-b8ae-ef6458f41153")
                                    .execute()
                                    .body()
                            )
                            if (userinfo["code"].toString().toInt() == 200) {
                                val parseObj = JSONUtil.parseObj(userinfo["data"])
                                thread {
                                    val intent =
                                        Intent(this@LoginActivity, MainActivity::class.java)
                                    intent.putExtra("username", username.text.toString())
                                    intent.putExtra(
                                        "petname",
                                        parseObj["petname"].toString()
                                    )
                                    intent.putExtra(
                                        "gender",
                                        parseObj["gender"].toString().toInt()
                                    )
                                    intent.putExtra(
                                        "description",
                                        parseObj["description"].toString()
                                    )
                                    intent.putExtra(
                                        "img",
                                        parseObj["img"].toString()
                                    )
                                    startActivity(intent)
                                    this@LoginActivity.finish()
                                    tip.dismiss()
                                }
                            }

                        }

                    }

                }

                tip.dismiss()
            }
        }
    }
}